package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.dto.ResponseDto;
import com.student.mapper.ArtistsMapper;
import com.student.pojo.Artists;
import com.student.service.ArtistsService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.student.util.ChineseToPinyin.getFirstLetters;
import static com.student.util.ChineseToPinyin.getFirstLetterss;

/**
 * @BelongsProject: SupportGroup
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2025-02-28 12:35
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class ArtistsServiceImpl implements ArtistsService {
    @Autowired
    private ArtistsMapper artistsMapper;

    @Override
    public ResponseDto artistsMessage(Artists artists) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(artists.getCurrentPage(), artists.getPageSize());
        List<Artists> snackGoodsCates = artistsMapper.artistsMessage(artists);

        PageInfo<Artists> cyCardPageInfo = new PageInfo<>(snackGoodsCates);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto addArtistsMessage(Artists artists) {
        boolean firstLetterss = getFirstLetterss(artists.getName());
        if (!firstLetterss){
            artists.setShouzimu(getFirstLetters(artists.getName()));
        }else {
            artists.setShouzimu("#");
        }

        Integer res = artistsMapper.addArtistsMessage(artists);
        if (res >= 1) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto updateArtistsMessage(Artists artists) {
        boolean firstLetterss = getFirstLetterss(artists.getName());
        if (!firstLetterss){
            artists.setShouzimu(getFirstLetters(artists.getName()));
        }else {
            artists.setShouzimu("#");
        }

        Integer res=artistsMapper.updateArtistsMessage(artists);
        if (res >= 1) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteArtists(Integer id) {
        Integer res=artistsMapper.deleteArtists(id);
        if (res >= 1) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getAllArtist() {
        List<Artists> artists=artistsMapper.getAllArtist();
        if(artists.size()>0){
            return ResponseDto.success(artists);
        }
        return ResponseDto.error(null);
    }
}